.PHONY : all clean

DIR_BIN := bin
DIR_BOOT := boot
DIR_HEADER := lib/ lib/kernel/ lib/user/

CFLAGS := -fno-builtin

vpath %.h $(DIR_HEADER)

all : $(DIR_BIN) $(DIR_BIN)/hd60M.img
	nasm $(DIR_BOOT)/mbr.S -o $(DIR_BIN)/mbr.bin -I $(DIR_BOOT)/
	nasm $(DIR_BOOT)/loader.S -o $(DIR_BIN)/loader.bin -I $(DIR_BOOT)/
	nasm -f elf lib/kernel/print.S -o $(DIR_BIN)/print.o -I lib/
	nasm -f elf kernel/kernel.S -o $(DIR_BIN)/kernel.o -I lib/
	gcc -c kernel/main.c -o bin/main.o -Ilib/kernel/ $(CFLAGS)
	gcc -c kernel/interrupt.c -o bin/interrupt.o -Ilib/kernel/ $(CFLAGS)
	gcc -c device/timer.c -o bin/timer.o -Ilib/kernel/ $(CFLAGS)
	gcc -c kernel/init.c -o bin/init.o -Ilib/kernel/ $(CFLAGS)
	gcc -c kernel/debug.c -o bin/debug.o -Ilib/kernel/ $(CFLAGS)
	gcc -c kernel/memory.c -o bin/memory.o -Ilib/kernel/ $(CFLAGS)
	gcc -c thread/thread.c -o bin/thread.o -Ilib/kernel/ -Ilib/ $(CFLAGS)
	nasm -f elf thread/switch.S -o $(DIR_BIN)/switch.o -I lib/
	gcc -c lib/kernel/bitmap.c -o bin/bitmap.o -Ilib/kernel/ $(CFLAGS)
	gcc -c lib/kernel/list.c -o bin/list.o -Ilib/kernel/ -Ilib/ $(CFLAGS)
	gcc -c lib/string.c -o bin/string.o -Ilib/kernel/ -Ilib/ $(CFLAGS)
	ld bin/main.o bin/kernel.o bin/interrupt.o bin/timer.o bin/init.o bin/debug.o bin/memory.o bin/thread.o bin/switch.o bin/print.o bin/bitmap.o bin/list.o bin/string.o -Ttext 0xc0002000 -e main -o bin/kernel.bin
	
	dd if=$(DIR_BIN)/mbr.bin of=$(DIR_BIN)/hd60M.img bs=512 count=1 seek=0 conv=notrunc
	dd if=$(DIR_BIN)/loader.bin of=$(DIR_BIN)/hd60M.img bs=512 count=9 seek=1 conv=notrunc
	dd if=$(DIR_BIN)/kernel.bin of=$(DIR_BIN)/hd60M.img bs=512 count=200 seek=10 conv=notrunc
	
	ndisasm $(DIR_BIN)/loader.bin > $(DIR_BIN)/loader.dis
	

$(DIR_BIN)/hd60M.img :
	bximage -hd -mode="flat" -size=60 -q $@
	
$(DIR_BIN) :
	mkdir $@

clean :
	rm -rf $(DIR_BIN)
